package com.aquila.datas;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.content.Context;

public class DatabaseTools {
	//保存到sd卡的路径
	public static String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() 
			+"/.datab";
	private static String BIBLE_DATABASE_NAME = "bibleData.cfg";
	//手机内存的路径
//	public final static String DATABASE_PATH = android.os.Environment.getDataDirectory().getAbsolutePath()
//			+ "/data/com.aquila.bible/databases"; 
	public static boolean isDataExist(){
		File dir = new File(DATABASE_PATH);
		// 如果/sdcard/目录不存在，创建这个目录
		if (!dir.exists()) {
			if (!dir.mkdir()) {
            	DATABASE_PATH = android.os.Environment.getDataDirectory().getAbsolutePath() +"/data/com.aquila.bible/databases" ;
            	File nFile = new File(DATABASE_PATH) ;
            	if (!nFile.exists()) {
            		nFile.mkdir() ;
                }
			}
		} 
		String databaseFilename = DATABASE_PATH + "/" + BIBLE_DATABASE_NAME ;
		File file = new File(databaseFilename) ;
//		File lxfile = new File(lxdatabaseFilename);
//		File everyFile = new File(everydatabaseFilename);
		
		if (file.exists() && file.length() > 10000) {
            	return true ;
            }
		return false ;
	}
	
	
	/***
	 * @param context
	 * @param isCreate
	 * void
	 * 2012-11-22 下午5:31:52
	 */
	public static boolean updataDataBase(Context context, boolean isCreate){
		String databaseFilename = DATABASE_PATH + "/" + BIBLE_DATABASE_NAME;
		File file = new File(databaseFilename) ;
		if (file.exists()) {
			file.delete() ;
		}
		
		// 获得封装tongcheng.db文件的InputStream对象
        try {
        	InputStream is = context.getAssets().open("main_data.mp3") ;
//        	InputStream is = context.getResources().openRawResource(
//        			R.raw.main_data);
        	FileOutputStream fos;
	        fos = new FileOutputStream(file, true);
	    	byte[] buffer = new byte[8192];
			int count = 0;
			// 开始复制tongcheng.db文件
			while ((count = is.read(buffer)) > 0) {
				fos.write(buffer, 0, count);
			}
			fos.close();
			is.close();
        }
        catch (Exception e) {
        	e.printStackTrace();
        	return false ;
        }
        return true ;
	}
	
//		/**
//		 *Administrator
//		 *2012-11-10 下午2:33:24
//		 */
//		public static void updateEveryDataBase(Context context, boolean isCreate){
//			String databaseFilename = DATABASE_PATH + "/" + SystemConfig.EVERY_LX_DATABASE_NAME;
//			File file = new File(databaseFilename) ;
//			if (file.exists()) {
//				file.delete() ;
//			}
//			// 获得封装tongcheng.db文件的InputStream对象
//			try {
////			InputStream is = context.getAssets().open("everylx.mp3") ;
//			InputStream is = context.getResources().openRawResource(R.raw.everylx);
//			FileOutputStream fos;
//			fos = new FileOutputStream(file, true);
//			byte[] buffer = new byte[8192];
//			int count = 0;
//			// 开始复制文件
//			while ((count = is.read(buffer)) > 0) {
//			fos.write(buffer, 0, count);
//			}
//			fos.close();
//			is.close();
//			}
//			catch (Exception e) {
//			e.printStackTrace();
//			}
//			
//	}
}
